//对套接字的控制器

import java.net.InetAddress;    
import java.net.DatagramSocket; 
import java.net.DatagramPacket; 
import java.io.IOException;

public class MySocketController{
	private static DatagramSocket socket = null;
	private static DatagramPacket packet = null;
	private static byte[] dataByte = null;
	
	public MySocketController(){}
	
	public MySocketController(DatagramSocket socket)throws IOException{
		this.socket = socket;
		dataByte = new byte[1024];
	}
	
	public void setSocket(DatagramSocket socket){
		this.socket = socket;
	}
	
	public DatagramSocket getSocket()throws IOException{
		return socket;
	}
	
	public DatagramPacket getPacket(){
		return packet;
	}
	
	public void setPacket(DatagramPacket packet){
		this.packet = packet;
	}
	
	public String getContent(final int timeout)throws IOException {
		packet = new DatagramPacket(dataByte,dataByte.length);
		socket.setSoTimeout(timeout);
		socket.receive(packet);
		return new String(packet.getData(), 0, packet.getLength());
	}
	
	public String getContent()throws IOException {
		return getContent(0);
	}
	
	public void setContent(String s, int port,String ip)throws IOException{
		int i;
		for(i=0;i<s.getBytes().length;++i)
		{
			dataByte[i]=s.getBytes()[i];
		}
		packet = new DatagramPacket(dataByte, i, InetAddress.getByName(ip), port);
		socket.send(packet);
	}
}


/*
private static final int PORT = 5188;
	private static final String IP = "127.0.0.1";
	
	private DataInputStream input = null;
	private DataOutputStream output = null;
	
	private static Socket socket = null;
	
	public Client(){
		
		try {
			socket = new Socket(IP, PORT);
			input = new DataInputStream(socket.getInputStream());
		  output = new DataOutputStream(socket.getOutputStream());
		} catch (Exception e) {
			MainFrame.getError("Client constructor!Error!");
		}
		
		MainFrame m = new MainFrame();
		m.setVisible(true);
	}
	
	public void setMessage(String s){
		try {
			output.writeUTF(s);
		} catch (Exception e) {
			MainFrame.getError("setMessage!Error!");
		}
		
	}
	
	public String getMesssage(){
		try {
			return input.readUTF();
		} catch (Exception e) {
			MainFrame.getError("getMessgae!Error!");
			return null;
		}
	}
	
	public Socket getSocket(){
		return socket;
	}
	
	public void closeSocket(){
		try {
			socket.close();
		} catch (Exception e) {
			socket = null;
			MainFrame.getError("closeSocket!Error!");
		}
	}
	
}
*/